layui.use(['table', 'util', 'layer'], function(){
    var table = layui.table,
        util = layui.util,
        layer = layui.layer;

    var userPaperTable = table.render({
        elem: '#testTable'
        ,url:'/userPaper/selectByPage'
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,title: '考试历史数据表'
        ,method: 'get'
        ,limits: [10,15,20,30]
        ,limit: 10
        ,id:'testTable'
        ,height: 523
        ,cellMinWidth: 80
        ,cols: [[
            {field:'id', title:'序号', align:'center',type: 'numbers', width: 100, unresize: true, sort: true}
            ,{field:'pname', title:'试卷名',align:'center'}
            ,{field:'uname', title:'姓名',align:'center'}
            ,{field:'upposttime', title:'交卷时间',align:'center', templet: function (res) {
                    return util.toDateString(res.upposttime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field:'upChoicescore', title:'选择题得分',align:'center'}
            ,{field:'upShortscore', title:'简答题得分',align:'center'}
            ,{field:'upscore', title:'总得分',align:'center', templet: function (res) {
                    return (res.upChoicescore + res.upShortscore) < (res.pscore * 0.6) ? "<span style='color: red;'>" + (res.upChoicescore + res.upShortscore) + "</span>" : (res.upChoicescore + res.upShortscore);
                }}
            ,{field:'pscore', title:'试卷总分',align:'center'}
            ,{field:'upStatus', title:'是否评阅',align:'center', templet: function (res) {
                    return res.upStatus == "0" ? "<span style='color: red;'>未批阅</span>" : "<span>已批阅</span>";
                }}
            ,{field:'aname', title:'阅卷人',align:'center'}
            ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo' }
        ]]
        ,page: true
        ,response: {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": []
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.data.code, //解析接口状态
                "msg": res.data.msg, //解析提示文本
                "count": res.data.count, //解析数据长度
                "data": res.data.data //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        switch(obj.event){
            case 'reloadPage':
                var pageIndex = $(".layui-laypage-em").next().html();
                userPaperTable.reload({
                    method:'get'
                    ,page: {
                        curr: pageIndex //重新从第 当前页 开始
                    }
                });
                break;
        };
    });


    $('.do_search').on('click', function () {
        // 搜索条件
        var pName=$(".pName").val();
        var uName=$(".uName").val();
        var aId=$(".aId").val();
        var upStatus=$(".upStatus").val();
        var score_min=$(".score_min").val();
        var score_max=$(".score_max").val();
        if ((score_min != "" || score_max != "")){
            if (isNaN(score_min)){
                layer.tips('请输入正确的数字', '.score_min', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
            if (isNaN(score_max)){
                layer.tips('请输入正确的数字', '.score_max', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
        }
        if ((score_min == "" && score_max != "")){
            score_min = 0;
        }
        if (score_min != "" && score_max == ""){
            score_max = 100;
        }
        if (score_min != "" && score_max != ""){
            var min = Number(score_min);
            var max = Number(score_max);
            if (min > max) {
                layer.tips('最低分不能高于最高分', '.score_max', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
        }
        userPaperTable.reload({
            method:'get'
            ,where: { //设定异步数据接口的额外参数，任意设
                pName: pName,
                uName: uName,
                aId: aId,
                upStatus: upStatus,
                score_min: score_min,
                score_max: score_max
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });

    });


    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'reviewPaper'){
            window.location.href="/reviewPaper/" + data.upId;
        }
    });
});